package com.example.waterdispenserm2.dp;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;

import com.example.waterdispenserm2.util.MyLog;
import com.example.waterdispenserm2.util.SPUtils;
import com.example.waterdispenserm2.bean.M2UIItem;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class M2NoodleHelperUtil {

    private  static M2NoodleHelperUtil  orderHelperUtil;

    private static final String TAG = "orderHelperUtil:";
    private static final String table = "tbname";
    SQLiteDatabase db;

    public static M2NoodleHelperUtil getInstance() {
        if (orderHelperUtil == null) {
            orderHelperUtil = new M2NoodleHelperUtil();
        }
        return orderHelperUtil;
    }

    public void init(){
        M2NoodleHelper openHelper= new M2NoodleHelper(SPUtils.getContext());
        db = openHelper.getWritableDatabase();
    }

    public void setSQ(M2UIItem orderStatus) {    //增
        ContentValues cv=new ContentValues();
        cv.put("tag", orderStatus.getTag());
        cv.put("name", orderStatus.getName());
        cv.put("taste", orderStatus.getTaste());
        cv.put("durationWater", orderStatus.getDurationWater());
        cv.put("heatingDuration", orderStatus.getHeatingDuration());
        cv.put("waterTemperature", orderStatus.getWaterTemperature());
        cv.put("amountWater", orderStatus.getAmountWater());
        //分别插入id name age三个数据，然后调用insert方法进行插入
        db.insert(table, null, cv);
    }

    public int getDel(String id) {  //删
        //这里仅演示根据ID删除某条数据
      int deleteCount =  db.delete(table, "id=?", new String[]{id});
        return deleteCount;
    }


    public static List<M2UIItem> OrderList =new ArrayList<>();
    public List<M2UIItem> getSQ() {    //查
        List<M2UIItem> list=new ArrayList<>();
        list.clear();
        OrderList.clear();
        //创建游标 cursor 并用游标遍历所有数据
        Cursor cursor = db.query(table, null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            M2UIItem orderStatus =new M2UIItem();
            orderStatus.setTag(  cursor.getString(0));
            orderStatus.setName( cursor.getString(1));
            orderStatus.setTaste(  cursor.getString(2));
            orderStatus.setDurationWater(  cursor.getString(3));
            orderStatus.setHeatingDuration(  cursor.getString(4));
            orderStatus.setWaterTemperature(  cursor.getString(5));
            orderStatus.setAmountWater(  cursor.getString(6));
            list.add(orderStatus);
        }

        OrderList=list;
        cursor.close();//关闭游标（cursor）释放资源
        return OrderList;
    }


    public void getUpData(String name,String taste,String durationWater,String heatingDuration,String waterTemperature,String amountWater,String tag) {//改

        ContentValues cv_Updata=new ContentValues();
        cv_Updata.put("name",name);
        cv_Updata.put("taste",taste);
        cv_Updata.put("durationWater",durationWater);
        cv_Updata.put("heatingDuration",heatingDuration);
        cv_Updata.put("waterTemperature",waterTemperature);
        cv_Updata.put("amountWater",amountWater);
        int a=  db.update(table, cv_Updata, "tag=?", new String[]{tag});
        MyLog.e(TAG, "修改:--"+tag+"--"+a);
    }
}

